package cn.jobstart.spider.downloader.request;

import cn.jobstart.common.BeanUtils;
import cn.jobstart.spider.AbstractTalentSearch;
import us.codecraft.webmagic.Request;

import java.lang.reflect.ParameterizedType;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-11-23 14:13
 */
public abstract class AbstractTalentRequest<T extends AbstractTalentRequest, S extends AbstractTalentSearch> extends Request {




    public AbstractTalentRequest(String url) {
        super(url);
    }

    public abstract String url();

    /**
     * 请求方式
     *
     * @return
     */
    public abstract String method();


    public Class<S> getSearchClass() {
        try {
            ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
            return (Class) type.getActualTypeArguments()[1];
        } catch (Exception e) {

            ParameterizedType type = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
            return (Class) type.getActualTypeArguments()[1];
        }
    }

    public Class<T> getRequestClass() {
        try {
            ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
            return (Class) type.getActualTypeArguments()[0];
        } catch (Exception e) {

            ParameterizedType type = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
            return (Class) type.getActualTypeArguments()[0];
        }
    }


    public void buildExtra(S s) {

    }


    /**
     * 创建对象
     *
     * @param s
     * @param
     * @return
     */
    public final void build(S s) throws Exception {
        S search = getSearchClass().newInstance();
        BeanUtils.copyPropertiesIgnoreNull(s, search);
        buildExtra(search);
        setMethod(getMethod());

    }


}
